package com.chainyoung.common.appcommon.ui;

import android.support.annotation.NonNull;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chainyoung.common.appcommon.entity.CommonList;
import com.chainyoung.common.base.BaseFragment;
import com.chainyoung.common.constant.LibConstant;
import com.chainyoung.common.di.AppComponent;
import com.chainyoung.common.mvp.BasePresenter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;

import java.util.List;

public abstract class BaseListFragment<K, T extends BasePresenter> extends BaseFragment<T> implements CommonListContract.View<K> {

    RecyclerView rvList;
    SmartRefreshLayout refreshLayout;

    protected int pageNo = 1;
    protected List<K> mData;
    private boolean isCanloadMore;
    protected BaseQuickAdapter adapter;
    private DividerItemDecoration dividerItemDecoration;
    private boolean isUseDefaultItemDecoration;


    protected void setView(RecyclerView recyclerView, SmartRefreshLayout refreshLayout) {
        rvList = recyclerView;
        this.refreshLayout = refreshLayout;
    }

    protected void setView(RecyclerView recyclerView, SmartRefreshLayout refreshLayout, DividerItemDecoration itemDecoration, boolean isDefaultItemdecoration) {
        rvList = recyclerView;
        this.refreshLayout = refreshLayout;
        this.dividerItemDecoration = itemDecoration;
        this.isUseDefaultItemDecoration = isDefaultItemdecoration;
    }


    @Override
    protected void initUi() {
        super.initUi();
        initDataType();

        LinearLayoutManager layoutManager = new LinearLayoutManager(mContext);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        if (dividerItemDecoration != null) {
            rvList.addItemDecoration(dividerItemDecoration);
        } else if (isUseDefaultItemDecoration) {
            rvList.addItemDecoration(new DividerItemDecoration(mContext, DividerItemDecoration.VERTICAL));
        }
        rvList.setLayoutManager(layoutManager);
        rvList.setAdapter(getAdapter());

        refreshLayout.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                if (isCanloadMore) {
                    pageNo++;
                    loadData();
                } else {
                    refreshLayout.finishLoadMore();
                }
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                pageNo = 1;
                loadData();
            }
        });

    }

    protected abstract RecyclerView.Adapter getAdapter();

    protected abstract void initDataType();


    protected abstract int getLayoutId();


    @Override
    protected void initData() {
        loadData();
    }


    @Override
    public void onGetListFailed(int errorCode, String errMessage) {
        refreshLayout.finishRefresh();
        refreshLayout.finishLoadMore();
        if (useLoadSir()) {
            showPageError();
        }


    }

    protected abstract void loadData();


    @Override
    public void onGetListSuccess(CommonList<K> data) {
        refreshLayout.finishRefresh();
        refreshLayout.finishLoadMore();
        if (data != null) {
            if (pageNo == 1) {
                mData.clear();
            }
            mData.addAll(data.getList());
            if (data.getList().size() == LibConstant.PAGE_SIZ) {
                isCanloadMore = true;
                refreshLayout.setEnableLoadMore(true);
            } else {
                isCanloadMore = false;
                refreshLayout.setEnableLoadMore(false);
            }
            adapter.notifyDataSetChanged();
        }
        if (mData.isEmpty() && useLoadSir()) {
            showPageEmpty();
        }
    }

    @Override
    protected void onPageRetry(View v) {
        pageNo = 1;
        initData();
    }
}
